/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com;

import java.io.IOException;
import java.net.Socket;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 
 * @author Java
 */
public class MethodFactory {

	private Socket socket;

	public MethodFactory(Socket socket) throws IOException {
		this.socket = socket;
	}

	private Request req;

	public Request getReq() {
		return req;
	}

	public Handler getRequest() throws IOException {
		Request req = HttpParser.parse(socket, Communicator.getRoot());
		System.out.println(req.getMethodHttp());
		System.out.println(req.getPathHttp());
		System.out.println(req.getVersionHttp());
		System.out.println(req.getSoket());
		Logger log = new Logger();
		log.writeLog("----------------\n");
		DateFormat dateFormat = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		String output = dateFormat.format(date);
		log.writeLog("Time of Request*"+output+"\n");
		log.writeLog("Address: "+socket.getInetAddress().toString() + " "
				+"Port: "+ socket.getPort()+"\n");
		
		log.writeLog(req.toString()+"\n");
	
		
		log.closeConnection();

		if ("GET".equals(req.getMethodHttp())) {
			return new GetMethod(req);

		} else if ("POST".equals(req.getMethodHttp())) {
			return new PostMethod(req);
		} else {
			req.setMethodHttp("GET");
			req.setPathHttp(".\\MyPrograms\\error.html");
			req.setVersionHttp("HTTP/1.1");
		}
		return new GetMethod(req);
	}
}